package cn.com.oceansoft.business.os.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.os.entity.Game;
import cn.com.oceansoft.business.os.entity.ShowResult;
import cn.com.oceansoft.business.os.service.IGameService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by lenovo on 2018/1/3.
 */
@Controller
@RequestMapping("game")
public class GameController extends BaseController {
    @Resource
    private IGameService gameService;
    @RequestMapping(value = "index")
    public String openRoleIndex(Query query, Model model) {
        model.addAttribute("query", query);
        model.addAttribute("pager", gameService.findListByPage(query));
        log(String.format("查看游戏组列表%d页", query.getPageNum()));
        return "os/game/index";
    }
    @RequestMapping(value = "add")
    public String openAddGame() {
        return "os/game/game_new";
    }
    @ResponseBody
    @RequestMapping(value = "new", method = POST)
    public Result addGame(Game game, HttpServletRequest request) throws Exception {
        if(!"".equals(game.getDbid())&&null!=game.getDbid()){
            return gameService.update(game);

        }else{
            return gameService.save(game);
        }

    }
    @RequestMapping(value = "view/{dbid}")
    public String viewRole(@PathVariable("dbid") String  dbid, Model model) {
        model.addAttribute("game",gameService.findOne(dbid));
        return "os/game/game_new";
    }
    @ResponseBody
    @RequestMapping(value = "{dbid}")
    public Result deleteRole(@PathVariable("dbid") String  dbid) {
        return gameService.delete(dbid);
    }
    @RequestMapping(value = "points/{dbid}")
    public String resultLuckyBoy(@PathVariable("dbid") String  dbid, Model model) {
        model.addAttribute("game", gameService.findOne(dbid));
        return "os/game/game_points";
    }
    @ResponseBody
    @RequestMapping(value = "jf", method = POST)
    public Result jf(Game game, HttpServletRequest request) throws Exception {
            String old=request.getParameter("old");
            if(!"".equals(old)&&null!=old){
                game.setScore(Integer.parseInt(old)+Integer.parseInt(game.getScore())+"");
            }
            return gameService.update(game);


    }
    @RequestMapping(value = "game-r")
    public String openGameResult(Query query, Model model) {
        Pager<Game> pager = gameService.findListByPage(query);
        model.addAttribute("pager", pager);
        return "os/game/game_r";
    }

    @ResponseBody
    @RequestMapping(value = "game-result")
    public JSONArray openResult(HttpServletRequest resquest,Query query) {
        query.setNumPerPage(99999);
        List<Game> res = gameService.findListByPage(query).getList();
        for(Game g:res){
            if("".equals(g.getScore())||null==g.getScore()){
                g.setScore("0");
            }
        }
        return JSONArray.parseArray(JSON.toJSONString(res));
    }
}
